package handler

import (
	db "echo_prj/db"
	"fmt"
	"github.com/labstack/echo"
	"log"
	"strconv"
	"time"
)

//获取招聘
func GetRecruit(c echo.Context) error {
	var a = Res{}
	as,err := db.GetRecruit()
	if err != nil{
		log.Println("GetRecruit,err=",err)
		a.Code=-1
		a.Msg=err.Error()
	}else{
		a = Res{
			Code:200,
			Msg:"",
			Result:as,
		}
	}
	return c.JSON(200,a)
}


//发布招聘
func PostRecruit(c echo.Context) error {
	var a = Res{}
	position := c.FormValue("position")
	content := c.FormValue("content")
	releaseDate := time.Now().Format("2006-01-02")
	id,err := db.PostRecruit(position,content,releaseDate)
	if err != nil{
		log.Println("PostRecruit,err=",err)
		a.Code=-1
		a.Msg=err.Error()
	}else{
		a = Res{
			Code:200,
			Msg:"",
			Result:id,
		}
	}
	return c.JSON(200,a)
}

//修改招聘
func EditRecruit(c echo.Context) error {
	var a = Res{}
	position := c.FormValue("position")
	content := c.FormValue("content")
	uid := c.FormValue("id")
	id,err := db.EditRecruit(position,content,uid)
	if err != nil{
		log.Println(err)
		a.Code=-1
		a.Msg=err.Error()
	}else{
		a = Res{
			Code:200,
			Msg:"",
			Result:id,
		}
	}
	return c.JSON(200,a)
}
//删除招聘
func DelRecruit(c echo.Context) error {
	var a = Res{}
	uid, err := strconv.Atoi(c.FormValue("id"))
	if err != nil {
		log.Println("DelRecruit,err=",err)
		a.Code=-1
		a.Msg="参数错误"
		return c.JSON(200,a)
	}

	id,err := db.DelRecruit(fmt.Sprint(uid))
	if err != nil{
		log.Println("DelRecruit,err=",err)
		a.Code=-1
		a.Msg=err.Error()
	}else{
		a = Res{
			Code:200,
			Msg:"",
			Result:id,
		}
	}
	return c.JSON(200,a)
}